package com.chixing.day11_exception.work;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateDemo {
    public static void main(String[] args) throws ParseException {
        //获得当前时间
        Date date1 = new Date();
        System.out.println(date1);

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime());
        //获得2天以后的日期
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)+2);
        System.out.println(calendar.getTime());

        LocalDateTime localDateTime1 = localDateTime.plusDays(2);
        System.out.println("两天后："+localDateTime1);
        //获得3天前的日期
        LocalDateTime localDateTime2 = localDateTime.plusDays(-3);
        System.out.println("三天前："+localDateTime2);
        //将字符串"2022/7/21 19:30" 转换成日期对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/dd HH:mm");
        LocalDateTime parse = LocalDateTime.parse("2022/7/21 19:30", formatter);
        System.out.println(parse);
        
        SimpleDateFormat str1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date2 = str1.parse("2022/9/21 19:30");
        System.out.println("字符串"+date2);
        //比较两天日期对象date  相差的天数
        /*long phase = (Math.abs(date1.getTime() - date2.getTime()))/1000/60/60/24;
        System.out.println("相差了"+phase+"天");*/


        LocalDate localDate1 = LocalDate.now();
        LocalDate localDate2 = LocalDate.of(2022, 7, 21);
        Period period = Period.between(localDate1,localDate2);
        int phase1 = (Math.abs(period.getDays())+(Math.abs(period.getMonths())*30)+(Math.abs(period.getYears())*365));
        System.out.println("相差的日："+ phase1);
    }
}
